//
//  SelectionViewController.m
//  iPicker
//
//  Created by Admin on 17-12-10.
//  Copyright 2010 Bedrijf. All rights reserved.
//

#import "SelectionViewController.h"
#import "PeriodDetailsViewController.h"
#import "Selection.h"
#import "DBAccess.h"

@implementation SelectionViewController
@synthesize sYear;
@synthesize sPeriod;
@synthesize selection1;

- (IBAction) buttonReturn:(id) sender
{
	[self.view removeFromSuperview];
}

#pragma mark Table View Controller Methods
- (void) doSomething:(NSString *)year: (NSString *)period
{
	self.sYear = year;
	self.sPeriod = period;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    
	// get de DBAccess Object
	DBAccess *dbAccess = [[DBAccess alloc] init];
	
	//get the selection array from the database
	self.selection1 = [dbAccess getAllPeriods:sYear];
	
	//close the database
	[dbAccess closeDatabase];
	
	//release the database to free memory
	[dbAccess release];
}




// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[sYear release];
	[sPeriod release];
	[selection1 release];
    [super dealloc];
}

#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [self.selection1 count];
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *CellIndentifier = @"Cell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
	
	if (cell == nil) 
	{
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier] autorelease];
	}
	
	//configure the cell
	//get the selection object
	Selection *selections = [self.selection1 objectAtIndex:[indexPath row]];
	
	cell.textLabel.text = selections.name;
	return cell;
}

#pragma mark -
#pragma mark Table Delegate Methods

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSInteger row = [indexPath row];
	if(row == 0)
		return nil;
	
	return indexPath;					 
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{	
	Selection *selection = [self.selection1 objectAtIndex:[indexPath row]];
	
	PeriodDetailsViewController *detailsView = [[PeriodDetailsViewController alloc] initWithNibName:@"detailsView" bundle:nil]; 		
	[detailsView setLabelsForPeriod:selection];
	
	
	[self.view addSubview:detailsView.view];
	
	//[detailsViewController1 release];
}
@end
